package com.cleaningcloud.erp.service.impl;

import java.util.List;

import com.cleaningcloud.cld.api.domain.CldRecruitPlan;
import com.cleaningcloud.cld.api.domain.CldStaff;
import com.cleaningcloud.common.core.constant.UserConstants;
import com.cleaningcloud.common.core.exception.ServiceException;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.core.utils.UniqueIdUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.domain.excelExport.CustomerExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.erp.mapper.ErpCustomerMapper;
import com.cleaningcloud.erp.api.domain.ErpCustomer;
import com.cleaningcloud.erp.service.IErpCustomerService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户管理Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2024-10-02
 */
@Service
public class ErpCustomerServiceImpl implements IErpCustomerService 
{
    @Autowired
    private ErpCustomerMapper erpCustomerMapper;

    /**
     * 查询客户管理
     * 
     * @param customerId 客户管理主键
     * @return 客户管理
     */
    @Override
    public ErpCustomer selectErpCustomerByCustomerId(Long customerId)
    {
        return erpCustomerMapper.selectErpCustomerByCustomerId(customerId);
    }

    /**
     * 查询客户管理列表
     * 
     * @param erpCustomer 客户管理
     * @return 客户管理
     */
    @Override
    public List<ErpCustomer> selectErpCustomerList(ErpCustomer erpCustomer)
    {
        return erpCustomerMapper.selectErpCustomerList(erpCustomer);
    }

    @Override
    public List<ErpCustomer> getErpCustomerList() {
        return erpCustomerMapper.getErpCustomerList();
    }

    /**
     * 新增客户管理
     * 
     * @param erpCustomer 客户管理
     * @return 结果
     */
    @Override
    public int insertErpCustomer(ErpCustomer erpCustomer)
    {
        erpCustomer.setCreateTime(DateUtils.getNowDate());
        erpCustomer.setCreateId(SecurityUtils.getUserId());
        erpCustomer.setCreateName(SecurityUtils.getUsername());
        erpCustomer.setFlag("Y");
        erpCustomer.setCustomerNo("KH" + UniqueIdUtils.generateUniqueId());
        erpCustomer.setApproveStatus(0);//待审核
        return erpCustomerMapper.insertErpCustomer(erpCustomer);
    }

    /**
     * 修改客户管理
     * 
     * @param erpCustomer 客户管理
     * @return 结果
     */
    @Override
    public int updateErpCustomer(ErpCustomer erpCustomer)
    {
        return erpCustomerMapper.updateErpCustomer(erpCustomer);
    }

    /**
     * 批量删除客户管理
     * 
     * @param customerIds 需要删除的客户管理主键
     * @return 结果
     */
    @Override
    public int deleteErpCustomerByCustomerIds(Long[] customerIds)
    {
        return erpCustomerMapper.deleteErpCustomerByCustomerIds(customerIds);
    }

    /**
     * 删除客户管理信息
     * 
     * @param customerId 客户管理主键
     * @return 结果
     */
    @Override
    public int deleteErpCustomerByCustomerId(Long customerId)
    {
        return erpCustomerMapper.deleteErpCustomerByCustomerId(customerId);
    }

    /**
     * 导出客户管理列表
     *
     * @param CustomerExport 客户导出
     * @return 客户管理
     */
    @Override
    public List<CustomerExport> exportErpCustomerList(CustomerExport customerExport)
    {
        return erpCustomerMapper.exportErpCustomerList(customerExport);
    }

    /**
     * @Author Shaw
     * @Description 注册客户信息
     * @date: 2025-04-02 11:07
     * @param: //TODO
     */
    @Override
    @Transactional
    public boolean registerCustomer(ErpCustomer erpCustomer) {
        erpCustomer.setLinkman(erpCustomer.getCustomerName());
        erpCustomer.setLinkmanPhone(erpCustomer.getUserName());
        erpCustomer.setCreateName(erpCustomer.getCustomerName());
        erpCustomer.setCreateTime(DateUtils.getNowDate());
        erpCustomer.setFlag("0");
        erpCustomer.setApproveStatus(0);//待审核
        erpCustomer.setCustomerNo("KH" + UniqueIdUtils.generateUniqueId());
        int i = erpCustomerMapper.insertErpCustomer(erpCustomer);
        return i > 0;
    }

    /**
     * @Author Shaw
     * @Description 校验手机号码(用户账号)是否唯一
     * @date: 2025-04-01 16:36
     * @param: //TODO
     */
    @Override
    public boolean checkUserNameUnique(ErpCustomer erpCustomer)
    {
        Long customerId = StringUtils.isNull(erpCustomer.getCustomerId()) ? -1L : erpCustomer.getCustomerId();
        ErpCustomer info = erpCustomerMapper.checkUserNameUnique(erpCustomer.getUserName());
        if (StringUtils.isNotNull(info) && info.getCustomerId().longValue() != customerId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * @Author Shaw
     * @Description 根据用户账号获取客户信息
     * @date: 2025-04-01 16:36
     * @param: //TODO
     */
    @Override
    public ErpCustomer getCustomerInfoByUserName(String userName)
    {
        return erpCustomerMapper.getCustomerInfoByUserName(userName);
    }

    /**
     * @Author Shaw
     * @Description 审核客户
     * @date: 2025-04-02 16:24
     * @param: //TODO
     */
    @Override
    @Transactional
    public int auditErpCustomer(ErpCustomer erpCustomer){
        ErpCustomer customer =  erpCustomerMapper.selectErpCustomerByCustomerId(erpCustomer.getCustomerId());
        if (null != customer) {
            customer.setApproveId(SecurityUtils.getUserId());
            customer.setApproveName(SecurityUtils.getUsername());
            customer.setApproveTime(DateUtils.getNowDate());
            customer.setApproveStatus(erpCustomer.getApproveStatus());
            if(erpCustomer.getApproveStatus() == 2){
                customer.setRejectTime(DateUtils.getNowDate());
                customer.setRejectReason(erpCustomer.getRejectReason());
            }
        }
        return erpCustomerMapper.updateErpCustomer(customer);
    }
}
